import { contextBridge, ipcRenderer } from 'electron';

// 暴露安全的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 文件操作API
  openFileDialog: () => ipcRenderer.invoke('open-file-dialog'),
  saveFileDialog: (data: ArrayBuffer, defaultFileName: string) => 
    ipcRenderer.invoke('save-file-dialog', data, defaultFileName),
  
  // 转换功能API
  convertCsvToXlsx: (csvData: string, options: any) =>
    ipcRenderer.invoke('convert-csv-to-xlsx', csvData, options),
  
  getCsvPreview: (csvData: string, options: any) =>
    ipcRenderer.invoke('get-csv-preview', csvData, options),
  
  detectFileEncoding: (buffer: Buffer) =>
    ipcRenderer.invoke('detect-file-encoding', buffer),
  
  getConverters: () => ipcRenderer.invoke('get-converters'),
  checkFormatSupport: (format: string) => ipcRenderer.invoke('check-format-support', format),
  
  // 应用控制API
  minimizeWindow: () => ipcRenderer.send('minimize-window'),
  maximizeWindow: () => ipcRenderer.send('maximize-window'),
  closeWindow: () => ipcRenderer.send('close-window'),
});

// 类型定义
export interface ElectronAPI {
  openFileDialog: () => Promise<any>;
  saveFileDialog: (data: ArrayBuffer, defaultFileName: string) => Promise<any>;
  convertCsvToXlsx: (csvData: string, options: any) => Promise<any>;
  getCsvPreview: (csvData: string, options: any) => Promise<any>;
  detectFileEncoding: (buffer: Buffer) => Promise<any>;
  getConverters: () => Promise<any>;
  checkFormatSupport: (format: string) => Promise<any>;
  minimizeWindow: () => void;
  maximizeWindow: () => void;
  closeWindow: () => void;
}

declare global {
  interface Window {
    electronAPI: ElectronAPI;
  }
}